// medal.js - 我的勋章页面逻辑
import { formatTime } from '../../utils/util';

Page({
  data: {
    // 勋章统计数据
    totalMedals: 20,
    collectedMedals: 8,
    progressPercentage: 40,
    
    // 当前选中的分类
    activeCategory: 'all',
    
    // 勋章数据列表
    medals: [
      {
        id: 'medal001',
        name: '单词大师',
        description: '累计学习1000个单词',
        imageUrl: '/images/medals/master.png',
        rarity: 'gold',
        category: 'learning',
        isCollected: true,
        collectedDate: '2023-05-15',
        currentProgress: 1000,
        requiredProgress: 1000,
        progressPercentage: 100,
        hint: ''
      },
      {
        id: 'medal002',
        name: '挑战之王',
        description: '连续7天完成每日挑战',
        imageUrl: '/images/medals/champion.png',
        rarity: 'gold',
        category: 'challenge',
        isCollected: true,
        collectedDate: '2023-05-20',
        currentProgress: 7,
        requiredProgress: 7,
        progressPercentage: 100,
        hint: ''
      },
      {
        id: 'medal003',
        name: '学习新星',
        description: '首次完成学习任务',
        imageUrl: '/images/medals/star.png',
        rarity: 'silver',
        category: 'learning',
        isCollected: true,
        collectedDate: '2023-05-01',
        currentProgress: 1,
        requiredProgress: 1,
        progressPercentage: 100,
        hint: ''
      },
      {
        id: 'medal004',
        name: '记忆达人',
        description: '连续5次单词测验满分',
        imageUrl: '/images/medals/memory.png',
        rarity: 'silver',
        category: 'learning',
        isCollected: false,
        collectedDate: '',
        currentProgress: 3,
        requiredProgress: 5,
        progressPercentage: 60,
        hint: '再连续获得2次满分即可解锁'
      },
      {
        id: 'medal005',
        name: '词汇收藏家',
        description: '收集10个不同主题的词汇',
        imageUrl: '/images/medals/collector.png',
        rarity: 'silver',
        category: 'collection',
        isCollected: false,
        collectedDate: '',
        currentProgress: 7,
        requiredProgress: 10,
        progressPercentage: 70,
        hint: '继续收集3个主题词汇即可解锁'
      },
      {
        id: 'medal006',
        name: '坚持达人',
        description: '连续学习30天',
        imageUrl: '/images/medals/persistence.png',
        rarity: 'gold',
        category: 'learning',
        isCollected: false,
        collectedDate: '',
        currentProgress: 15,
        requiredProgress: 30,
        progressPercentage: 50,
        hint: '已坚持学习15天，再坚持15天即可解锁'
      },
      {
        id: 'medal007',
        name: '限时挑战王',
        description: '在限时挑战中获得1000分以上',
        imageUrl: '/images/medals/timer.png',
        rarity: 'bronze',
        category: 'challenge',
        isCollected: true,
        collectedDate: '2023-05-18',
        currentProgress: 1250,
        requiredProgress: 1000,
        progressPercentage: 100,
        hint: ''
      },
      {
        id: 'medal008',
        name: '主题专家',
        description: '完成5个主题的全部单词学习',
        imageUrl: '/images/medals/expert.png',
        rarity: 'silver',
        category: 'collection',
        isCollected: true,
        collectedDate: '2023-05-12',
        currentProgress: 5,
        requiredProgress: 5,
        progressPercentage: 100,
        hint: ''
      },
      // 更多勋章数据...
    ],
    
    // 展示的勋章列表
    displayedMedals: [],
    
    // 勋章详情弹窗
    showDetailModal: false,
    selectedMedal: null
  },

  onLoad: function() {
    // 加载勋章数据
    this.loadMedalData();
  },

  onShow: function() {
    // 每次显示页面时刷新勋章数据
    this.loadMedalData();
  },

  // 加载勋章数据
  loadMedalData: function() {
    // 在实际应用中，这里会从服务器获取勋章数据
    // 目前使用本地模拟数据
    
    // 计算统计数据
    const collectedMedals = this.data.medals.filter(medal => medal.isCollected).length;
    const totalMedals = this.data.medals.length;
    const progressPercentage = Math.round((collectedMedals / totalMedals) * 100);
    
    // 更新显示的勋章列表
    this.updateDisplayedMedals();
    
    // 更新数据
    this.setData({
      collectedMedals,
      totalMedals,
      progressPercentage
    });
  },

  // 切换勋章分类
  switchCategory: function(e) {
    const category = e.currentTarget.dataset.category;
    this.setData({
      activeCategory: category
    });
    this.updateDisplayedMedals();
  },

  // 更新显示的勋章列表
  updateDisplayedMedals: function() {
    let filteredMedals = this.data.medals;
    
    // 根据选中的分类过滤勋章
    if (this.data.activeCategory !== 'all') {
      filteredMedals = this.data.medals.filter(medal => medal.category === this.data.activeCategory);
    }
    
    // 更新显示的勋章列表
    this.setData({
      displayedMedals: filteredMedals
    });
  },

  // 展示勋章详情
  showMedalDetail: function(e) {
    const medalId = e.currentTarget.dataset.id;
    const selectedMedal = this.data.medals.find(medal => medal.id === medalId);
    
    this.setData({
      selectedMedal,
      showDetailModal: true
    });
  },

  // 关闭勋章详情弹窗
  closeDetailModal: function() {
    this.setData({
      showDetailModal: false,
      selectedMedal: null
    });
  },

  // 分享页面
  onShareAppMessage: function() {
    return {
      title: '我在知识学习小程序中获得了 ' + this.data.collectedMedals + ' 枚勋章，快来看看吧！',
      path: '/pages/medal/medal',
      // imageUrl: '/images/share_medals.png'
    };
  },
  
  // 分享到朋友圈
  onShareTimeline: function() {
    return {
      title: '我在知识学习小程序中获得了 ' + this.data.collectedMedals + ' 枚勋章，快来看看吧！',
      query: 'from=timeline',
      // imageUrl: '/images/share_medals.png'
    };
  }
});